package com.softeem.mpdemo.dto;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.experimental.Accessors;

/**
 * 统一返回结果：前端请求服务端接口，后台处理完之后统一返回该结果对象
 */
@Schema(description = "统一返回结果")
@Data
@Accessors(chain = true) //开启链式编程访问
public class Result {

    /**
     * 状态码，表示接口请求后的处理状态，例如 0-成功  40004-请求数据不存在  50001-服务器内部错误
     */
    @Schema(description = "状态码，表示接口请求后的处理状态",example = "0",requiredMode = Schema.RequiredMode.REQUIRED)
    private Integer code;
    /**
     * 执行结果是否成功
     */
    @Schema(description = "执行结果是否成功,true-成功；false-失败",example = "true")
    private boolean success;
    /**
     * 附加文本消息
     */
    @Schema(description = "附加的文本信息",example = "执行成功")
    private String msg;
    /**
     * 附加返回数据，例如:单一对象，结果集等
     */
    @Schema(description = "附加返回数据，例如:单一对象，结果集等",example = "{id:1,name:\"admin\"}")
    private Object data;
    @Schema(description = "数据行数，为前端提供分页支持")
    private Integer count;

    //私有化构造器：防止在外界通过new创建对象
    private Result(){}

    //封装统一成功返回结果
    public static Result ok(){
        Result r = new Result();
        r.success = true;
        r.code = 0;
        r.msg = "请求成功";
        return r;
    }

    //封装统一失败返回结果
    public static Result fail(){
        Result r = new Result();
        r.success = false;
        r.code = -1;
        r.msg = "请求失败";
        return r;
    }

}
